package com.wang.head.servlet;

import java.io.IOException;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;


import com.alibaba.fastjson.JSONObject;
import com.wang.common.pojo.ApiResponse;
import com.wang.common.pojo.ApiResponseEnum;
import com.wang.common.servlet.BaseServlet;
import com.wang.common.util.ConstatFinalUtil;
import com.wang.users.pojo.AUsers;
import com.wang.users.pojo.AUsersEnum;
import com.wang.users.service.IUsersDbService;
import com.wang.users.service.impl.UsersDbServiceImpl;
import jakarta.servlet.ServletConfig;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;

/**
 * 第二个Servlet 只存储和管理员息息相关的信息 登陆后的操作
 *
 * @author wangshMac
 */
@WebServlet(urlPatterns = "/head/UsersHeadServlet")
public class UsersHeadServlet extends BaseServlet {
    private IUsersDbService usersDbService;

    @Override
    public void init(ServletConfig config) throws ServletException {
        super.init(config);
        UsersDbServiceImpl usersDbServiceImpl = new UsersDbServiceImpl();
        this.usersDbService = usersDbServiceImpl;

    }

    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        /* 接收参数 */
        String method = request.getParameter("method");
        if ("main".equalsIgnoreCase(method)) {
            /* 登陆后首页 */
            main(request, response);
        } else if ("usersUpdateSubmit".equalsIgnoreCase(method)) {
            /* 更新提交 */
            usersUpdateSubmit(request, response);
        }
    }

    /**
     * 更新
     *
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    private void usersUpdateSubmit(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        HttpSession session = request.getSession();
        ApiResponse<Object> apiResponse = new ApiResponse<Object>();
        /* 存储多个结果,Map存储 */
        Map<String, Object> dataMapJava = new HashMap<String, Object>();
        apiResponse.setDataMapJava(dataMapJava);

        /* 从session中获取信息 */
        AUsers users = (AUsers) this.findObjfromSession(request, "users");
        int usersId = users.getId();
        Map<String, Object> condMap = new HashMap<String, Object>();
        condMap.put("id", usersId);
        ApiResponse<AUsers> apiResponseRes = this.usersDbService.findOneUsersService(condMap);
        request.setAttribute("response", apiResponseRes.toJSON());

        users = apiResponseRes.getDataOneJava();

        boolean updateFlag = false;
        /* 根据不同的判断,页面跳转 */
        String operType = request.getParameter("operType");
        if ("update".equalsIgnoreCase(operType)) {
            String email = request.getParameter("email");
            String nickName = request.getParameter("nickName");
            String trueName = request.getParameter("trueName");
            String phone = request.getParameter("phone");
            String sex = request.getParameter("sex");

            users.setEmail(email);
            users.setNickName(nickName);
            users.setTrueName(trueName);
            users.setPhone(phone);
            users.setSex(Byte.valueOf(sex));

            updateFlag = true;
        }

        /* 更新 */
        if (updateFlag) {
            apiResponse = this.usersDbService.updateOneUsersService(users);
            session.setAttribute("users", users);
        }
        apiResponse.setInfo(
                ConstatFinalUtil.INFO_JSON.getString(apiResponseRes.getCode() + ""),
                Collections.EMPTY_MAP);

        this.updateFlag = "true";
        request.setAttribute("response", apiResponseRes.toJSON());
        this.main(request, response);
    }

    /**
     * 首页
     *
     * @param request
     * @param response
     * @throws IOException
     * @throws ServletException
     */
    private void main(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        /* 准备路径 */
        this.path = "/WEB-INF/jsp/back/admins/mainUsers.jsp";
        /* 从session中获取信息 */
        AUsers users = (AUsers) this.findObjfromSession(request, "users");
        int usersId = users.getId();
        Map<String, Object> condMap = new HashMap<String, Object>();
        condMap.put("id", usersId);
        ApiResponse<AUsers> apiResponse = this.usersDbService.findOneUsersService(condMap);
        if (this.updateFlag == null || "".equalsIgnoreCase(this.updateFlag)) {
            /* 清空信息 */
            apiResponse.clearInfo();
        }
        request.setAttribute("response", apiResponse.toJSON());
        /* 页面跳转 */
        request.getRequestDispatcher(path).forward(request, response);
    }
}